package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocCheckAddDTO;
import org.jsola.hr.dto.SocCheckUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.SocCheckCustomQuery;
import org.jsola.hr.query.SocCheckQuery;
import org.jsola.hr.service.ISocCheckService;
import org.jsola.hr.vo.SocCheckListVO;
import org.jsola.hr.vo.SocCheckParseImportListVO;
import org.jsola.hr.vo.SocCheckVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import javax.validation.Valid;


/**
 * 社保校验表
 *
 * @author zhr
 */
@RestController("hrSocCheckController")
@RequestMapping(HrConstants.API_PATH + "/v1/socChecks")
public class SocCheckController implements ISocCheckControllerAPI {

    @Autowired
    private ISocCheckService socCheckService;

    @Override
    @PostMapping
    public Result<SocCheckVO> save(@Valid @RequestBody SocCheckAddDTO socCheckAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保校验表权限校验

        return Result.success(
                socCheckService.save(socCheckAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocCheckUpdateDTO socCheckUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保校验表权限校验

        int count = socCheckService.update(socCheckUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{socCheckId}")
    public Result<Integer> delete(@PathVariable String socCheckId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保校验表权限校验

        int count = socCheckService.deleteByIds(tokenUser, socCheckId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socCheckIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保校验表权限校验

        int count = socCheckService.deleteByIds(tokenUser, socCheckIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socCheckId}")
    public Result<SocCheckVO> findById(@PathVariable String socCheckId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保校验表权限校验

        return Result.success(
                socCheckService.selectById(socCheckId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocCheckListVO>> listAll(@Valid SocCheckQuery socCheckQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保校验表权限校验

        return Result.success(
                socCheckService.select(socCheckQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocCheckListVO>> page(@Valid SocCheckQuery socCheckQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socCheckService.selectPage(socCheckQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/pageByMonthIdAndCheckType")
    public Result<Page<SocCheckVO>> pageByMonthIdAndCheckType(@Valid SocCheckCustomQuery socCheckCustomQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socCheckService.pageByMonthIdAndCheckType(socCheckCustomQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/parseImport")
    public Result<List<SocCheckParseImportListVO>> parseImport(@RequestParam("file") MultipartFile file) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 导入excel权限校验

        // 临时保存到本地
        String excelFilePath = FileKit.saveFileToLocal(file, null, null, new String[]{"xls", "xlsx"}, null);
        File excelFile = new File(excelFilePath);
        HrException.isTrue(excelFile.exists(), "文件上传失败");
        return Result.success(
                socCheckService.parseSocCheckImportFile(excelFile, tokenUser)
        );
    }

    @Override
    @PostMapping("/saveImport")
    public Result<List<SocCheckParseImportListVO>> saveImport(@RequestParam("file") MultipartFile file,
                                                              @Valid SocCheckAddDTO socCheckAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 导入excel权限校验

        // 临时保存到本地
        String excelFilePath = FileKit.saveFileToLocal(file, null, null, new String[]{"xls", "xlsx"}, null);
        File excelFile = new File(excelFilePath);
        HrException.isTrue(excelFile.exists(), "文件上传失败");
        return Result.success(
                socCheckService.saveSocCheckImportFile(excelFile, socCheckAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("/checkWebSiteDate")
    public Result<String> checkWebSiteDate(@Valid @RequestBody SocCheckCustomQuery socCheckCustomQuery) {
        TokenUser tokenUser = UserContext.getUser();

        socCheckService.checkWebSiteDate(socCheckCustomQuery, tokenUser);
        return Result.success();
    }


}

